﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kaleido.Util.Lang
{
    public class LangHelper
    {
        private static LangHelper _Lang = null;

        public static LangHelper Lang
        {
            get
            {
                if (_Lang == null)
                    _Lang = new LangHelper();
                return _Lang;
            }
        }

        public DynamicKey this[string key]
        {
            get
            {
                return new DynamicKey(key);
            }
        }

        public Dynamic Dynamic(object obj)
        {
            return new Dynamic(obj);
        }
    }

    public static class DynamicExtensions
    {
        public static IndexArray Array(params dynamic[] initValues)
        {
            var result = new IndexArray();
            foreach (var value in initValues)
            {
                result.Add(value);
            }
            return result;
        }

        public static IndexArray Array(this LangHelper o, params dynamic[] initValues)
        {
            return Array(initValues);
        }

        public static KeyArray Array(params KeyValuePair<string, dynamic>[] initValues)
        {
            var result = new KeyArray();
            foreach (var pair in initValues)
            {
                result[pair.Key] = pair.Value;
            }
            return result;
        }

        public static KeyArray Array(this LangHelper o, params KeyValuePair<string, dynamic>[] initValues)
        {
            return Array(initValues);
        }

        public static IndexArray Merge(IndexArray a1, IndexArray a2)
        {
            var values = a1.Values.Union(a2.Values);
            return Array(values);
        }

        public static IndexArray Merge(this LangHelper o, IndexArray a1, IndexArray a2)
        {
            return Merge(a1, a2);
        }

        public static KeyArray Merge(KeyArray a1, KeyArray a2)
        {
            var result = new KeyArray();
            foreach (var key in a1.Keys)
            {
                result[key] = a1[key];
            }
            foreach (var key in a2.Keys)
            {
                result[key] = a2[key];
            }
            return result;
        }

        public static KeyArray Merge(this LangHelper o, KeyArray a1, KeyArray a2)
        {
            return Merge(a1, a2);
        }
    }

}
